
//Copyright 1997-2009 Syrinx Development, Inc.
//This file is part of the Syrinx Web Application Framework (SWAF).
// == BEGIN LICENSE ==
//
// Licensed under the terms of any of the following licenses at your
// choice:
//
//  - GNU General Public License Version 3 or later (the "GPL")
//    http://www.gnu.org/licenses/gpl.html
//
//  - GNU Lesser General Public License Version 3 or later (the "LGPL")
//    http://www.gnu.org/licenses/lgpl.html
//
//  - Mozilla Public License Version 1.1 or later (the "MPL")
//    http://www.mozilla.org/MPL/MPL-1.1.html
//
// == END LICENSE ==
using System;
using System.Diagnostics;
using System.Collections;

using Swaf;
using Swaf.Config;

namespace Swaf.Script
{
	/// <summary>
	/// Summary description for NetScriptingConfigManager.
	/// </summary>
	public class NetScriptingConfigManager : IConfigPartner
	{
		protected IList m_assemblyRefs = new ArrayList();
		protected IList m_imports = new ArrayList();
		public NetScriptingConfigManager(IApplication app)
		{
		}

		internal void addAssemblyReference(string assemblyName)
		{
			m_assemblyRefs.Add(assemblyName);
		}
		internal void addImport(string importName)
		{
			m_imports.Add(importName);
		}

		internal IList AssemblyReferences {get{return m_assemblyRefs;}}
		internal IList PackageImports {get{return m_imports;}}

		#region IConfigPartner Members

		public void checkConfigStatus()
		{
			//Intentionally left blank.  We won't cause problems if no assemblies were referenced.  
			//Bad assembly references won't be known until a script is compiled that uses it.
		}

		public void registerForConfig(IConfigManager config)
		{
			config.registerHandler("...<NetScripting> <AssemblyReference>", new ScriptAssemblyRefConfigHandler(this));
			config.registerHandler("...<NetScripting> <PackageImport>", new ScriptPackageImportConfigHandler(this));
		}

		#endregion

		protected internal class ScriptAssemblyRefConfigHandler : ConfigHandler
		{
			protected static string[] s_legalAttributes = new string[]{};
			protected NetScriptingConfigManager m_mgr;
			public ScriptAssemblyRefConfigHandler(NetScriptingConfigManager mgr)
			{
				m_mgr = mgr;
			}
			public override void  executeBefore(IConfigElement configInfo)
			{
				configInfo.checkIllegalAttributes(s_legalAttributes);
				string name = configInfo.getChildText();
				name = Application.globalApp.resMgr.resolve(name);
			
				if(name != "")
					m_mgr.addAssemblyReference(name);
			}
		}

        protected internal class ScriptPackageImportConfigHandler : ConfigHandler
		{
			protected static string[] s_legalAttributes = new string[]{};
			protected NetScriptingConfigManager m_mgr;
			public ScriptPackageImportConfigHandler(NetScriptingConfigManager mgr)
			{
				m_mgr = mgr;
			}
			public override void  executeBefore(IConfigElement configInfo)
			{
				configInfo.checkIllegalAttributes(s_legalAttributes);
				string name = configInfo.getChildText();
				name = Application.globalApp.resMgr.resolve(name);
				if(name != "")
					m_mgr.addImport(name);
			}
		}
	}
}
